# Maintainer: George Rawlinson <grawlinson@archlinux.org>
# Contributor: Nogweii <me@nogweii.net>

pkgname=zk
pkgver=0.13.0
pkgrel=1
pkgdesc='A command-line tool helping you to maintain a Zettelkasten or personal wiki'
arch=('loong64' 'x86_64')
url='https://github.com/mickael-menu/zk'
license=('GPL3')
depends=('glibc')
makedepends=('git' 'go')
options=('!lto')
_commit='ea4457ad671aa85a6b15747460c6f2c9ad61bf73'
source=("$pkgname::git+$url.git#commit=$_commit")
md5sums=('SKIP')

pkgver() {
  cd "$pkgname"

  git describe --tags | sed 's/^v//'
}

prepare() {
  cd "$pkgname"

  # create directory for build output
  mkdir build

  # download dependencies
  export GOPATH="${srcdir}"
  go mod download
}

build() {
  cd "$pkgname"
  # set Go flags
  export CGO_CPPFLAGS="${CPPFLAGS}"
  export CGO_CFLAGS="${CFLAGS}"
  export CGO_CXXFLAGS="${CXXFLAGS}"
  export GOPATH="${srcdir}"

  go build -v \
    -buildmode=pie \
    -mod=readonly \
    -modcacherw \
    -ldflags "-compressdwarf=false \
    -linkmode external \
    -extldflags ${LDFLAGS} \
    -X main.Version=$(git describe --tags HEAD) \
    -X main.Build=$(git rev-parse --short HEAD)" \
    -tags "fts5" \
    -o build \
    .
}

check() {
  cd "$pkgname"
  go test -v -race -tags "fts5" ./...
}

package() {
  cd "$pkgname"

  # binary
  install -vDm755 -t "$pkgdir/usr/bin" build/zk

  # documentation
  install -vDm644 -t "$pkgdir/usr/share/doc/$pkgname" *.md
  cp -vr docs "$pkgdir/usr/share/doc/$pkgname"
}
